package com.example.controller;

import com.example.entity.Order;
import com.example.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;

    @PostMapping
    public String createOrder(@RequestParam String userId, @RequestParam BigDecimal amount) {
        Order order = new Order();
        order.setOrderId(System.currentTimeMillis());
        order.setUserId(userId);
        order.setAmount(amount);
        order.setCreateTime(LocalDateTime.now());
        order.setStatus("NEW");
        
        orderMapper.insert(order);
        return "订单创建成功：" + order.getOrderId();
    }

    @GetMapping("/{orderId}")
    public Order getOrder(@PathVariable Long orderId) {
        return orderMapper.findById(orderId);
    }

    @GetMapping("/user/{userId}")
    public List<Order> getUserOrders(@PathVariable String userId) {
        return orderMapper.findByUserId(userId);
    }
} 